package com.yunhe.estatemanage.service.impl;
import com.yunhe.common.util.BeansUtil;
import com.yunhe.estatemanage.domain.PaymentReminderConfiguration;
import com.yunhe.estatemanage.model.PaymentReminderConfigurationVo;
import com.yunhe.estatemanage.repository.PaymentReminderConfigurationRepository;
import com.yunhe.estatemanage.service.PaymentReminderConfigurationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.yunhe.common.util.CollectionUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @description payment_reminder_configuration
 * @author misty
 * @date 2021-09-06
 */
@Service
public class PaymentReminderConfigurationServiceImpl implements PaymentReminderConfigurationService {

    @Autowired
    private PaymentReminderConfigurationRepository paymentReminderConfigurationRepository;


    @Override
    public int createPaymentReminderConfiguration(List<PaymentReminderConfigurationVo> paymentReminderConfigurationVos) {
        if(!CollectionUtil.isEmpty(paymentReminderConfigurationVos)){
            paymentReminderConfigurationVos.forEach(paymentReminderConfigurationVo -> {
                PaymentReminderConfiguration paymentReminderConfiguration = new PaymentReminderConfiguration();
                BeanUtils.copyProperties(paymentReminderConfigurationVo,paymentReminderConfiguration);
                paymentReminderConfigurationRepository.createPaymentReminderConfigurationSelective(paymentReminderConfiguration);
            });
        }
        return 1;
    }


    @Override
    public int deletePaymentReminderConfiguration(int id) {
        return paymentReminderConfigurationRepository.deletePaymentReminderConfigurationByPrimaryKey(id);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updatePaymentReminderConfiguration(List<PaymentReminderConfigurationVo> paymentReminderConfigurationVos) {
        if(!CollectionUtil.isEmpty(paymentReminderConfigurationVos)){
            paymentReminderConfigurationVos.forEach(paymentReminderConfigurationVo -> {
                PaymentReminderConfiguration paymentReminderConfiguration = new PaymentReminderConfiguration();
                BeanUtils.copyProperties(paymentReminderConfigurationVo,paymentReminderConfiguration);
                if(paymentReminderConfiguration.getId() != null){
                    paymentReminderConfigurationRepository.updatePaymentReminderConfigurationByPrimaryKeySelective(paymentReminderConfiguration);
                }else {
                    paymentReminderConfigurationRepository.createPaymentReminderConfigurationSelective(paymentReminderConfiguration);
                }
            });
        }
        return 1;
    }


    @Override
    public PaymentReminderConfiguration findPaymentReminderConfigurationById(int id) {
        return paymentReminderConfigurationRepository.searchPaymentReminderConfigurationByPrimaryKey(id);
    }

    @Override
    public List<PaymentReminderConfigurationVo> findPaymentReminderConfigurationByStationId(Long realStationId) {
        List<PaymentReminderConfigurationVo> result = new ArrayList<>();
        List<PaymentReminderConfiguration> paymentReminderConfigurations = paymentReminderConfigurationRepository.
                findPaymentReminderConfigurationByStationId(realStationId);
        if(!CollectionUtil.isEmpty(paymentReminderConfigurations)){
            result = BeansUtil.listCopy(paymentReminderConfigurations,PaymentReminderConfigurationVo.class);
        }
        return result;
    }


}